<template>
    <div>
        <!--条件搜索-->
        <div class="search">
            <div class="column">
                <span>名称：</span>
                <el-input style="height: 21px;width: 136px" placeholder="供货商" v-model="searchForm.name"/>
            </div>
            <div class="column">
                <span>地址：</span>
                <el-input style="height: 21px;width: 136px" placeholder="地址" v-model="searchForm.address"/>
            </div>
            <div class="column">
                <span>描述：</span>
                <el-input style="height: 21px;width: 136px" placeholder="描述" v-model="searchForm.info"/>
            </div>
            <div class="column">
                <el-button type="success" @click="subSearchForm">搜索</el-button>
                <el-button type="danger" @click="newVisable=true">创建</el-button>
            </div>
        </div>
        <div class="table">
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    height="450px"
                    size="medium">
                <el-table-column
                        type="index"
                        width="200"
                        label="序号">
                </el-table-column>
                <el-table-column
                        prop="name"
                        :show-overflow-tooltip="true"
                        label="名称">
                </el-table-column>
                <el-table-column
                        prop="address"
                        :show-overflow-tooltip="true"
                        label="地址">
                </el-table-column>
                <el-table-column
                        prop="tel"
                        :show-overflow-tooltip="true"
                        label="联系电话">
                </el-table-column>
                <el-table-column
                        prop="info"
                        :show-overflow-tooltip="true"
                        label="备注">
                </el-table-column>
                <el-table-column
                        width="200px"
                        label="操作">
                    <template v-slot="scope">
                        <el-button type="warning" @click="editBtn(scope.row.cn)" plain>修改</el-button>
                        <el-button type="danger" @click="delBtn(scope.row.cn)" plain>删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pageUtils">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="searchForm.currentPage"
                        :page-sizes="[3, 5, 7, 9]"
                        :page-size="searchForm.pageSize"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="searchForm.total">
                </el-pagination>
            </div>
        </div>
        <!--创建弹出框-->
        <el-dialog
                title="创建供货商信息"
                :visible.sync="newVisable"
                width="50%">
            <el-form :model="newForm" :rules="rules" ref="newForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="newForm.name" placeholder="供应商名称"></el-input>
                </el-form-item>
                <el-form-item label="地址：" prop="address">
                    <el-input type="text" v-model="newForm.address" placeholder="供应商地址"></el-input>
                </el-form-item>
                <el-form-item label="电话：" prop="tel">
                    <el-input type="text" v-model="newForm.tel" placeholder="联系电话"></el-input>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input type="textarea" v-model="newForm.info" placeholder="供应商的简单描述"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitNewForm('newForm')">提交</el-button>
                    <el-button @click="()=>{
                        this.$refs['newForm'].resetFields();
                        newForm={};newVisable=false
                    }">关闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!--修改弹出框-->
        <el-dialog
                title="修改供货商信息"
                :visible.sync="editVisable"
                width="50%">
            <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="editForm.name" placeholder="供应商名称"></el-input>
                </el-form-item>
                <el-form-item label="地址：" prop="address">
                    <el-input type="text" v-model="editForm.address" placeholder="供应商地址"></el-input>
                </el-form-item>
                <el-form-item label="电话：" prop="tel">
                    <el-input type="text" v-model="editForm.tel" placeholder="联系电话"></el-input>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input type="textarea" v-model="editForm.info" placeholder="供应商的简单描述"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitEditForm('editForm')">提交</el-button>
                    <el-button @click="()=>{
                         this.$refs['editForm'].resetFields();
                        editForm={};editVisable=false}">关闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>


    </div>
</template>
<script>
    import {popup} from "@/assets/js/common";
    import {
        delSupplier,
        queryByCn,
        queryPageSupplier,
        saveSupplier,
        updateSupplier
    } from "@/api/inventory_management/supplier/supplierApi";

    export default {
        data() {
            return {
                tableData: [],
                editVisable: false,
                editForm: {},
                searchForm: {},
                newVisable: false,
                newForm: {},
                rules: {
                    name: [
                        {required: true, message: '供货商名称不能为空', trigger: 'blur'},
                    ],
                    address: [
                        {required: true, message: '供货商地址不能为空', trigger: 'blur'},
                    ],
                    tel: [
                        {required: true, message: '供货商联系电话不能为空', trigger: 'blur'},
                    ]

                }

            }
        },
        methods: {
            subSearchForm() {
                this.init()
            },
            init() {
                queryPageSupplier(this.searchForm).then(res => {
                    res = res.data
                    if (res.code == 200) {
                        this.tableData = res.data.records
                        this.searchForm.total = res.data.total
                        this.searchForm.pageSize = res.data.size
                        this.searchForm.currentPage = res.data.current
                    } else {
                        popup(res.msg, "error")
                    }
                })
            },
            /*分页*/
            handleSizeChange(val) {
                this.searchForm.pageSize = val
                this.init()
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.searchForm.currentPage = val
                this.init()
                console.log(`当前页: ${val}`);
            },
            submitSearchForm() {
                this.init()
            },
            submitNewForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        saveSupplier(this.newForm).then(res => {
                            res = res.data
                            if (res.code == 200) {
                                popup("添加成功")
                                this.newVisable = false
                                this.newForm = {}
                                this.init();

                            } else {
                                popup(res.msg, "error")
                            }
                        })
                    }
                })

            },
            delBtn(cn) {
                this.$confirm('确定要删除这条记录' + '？', '警示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delSupplier({cn: cn}).then(res => {
                        res = res.data
                        if (res.code == 200) {
                            popup("删除成功")
                            this.init()
                        } else {
                            popup(res.msg, "warning")
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });

            },
            editBtn(cn) {
                queryByCn({cn: cn}).then(res => {
                        res = res.data
                        if (res.code == 200) {
                            this.editForm = {...res.data}
                            this.editVisable = true
                        } else {
                            popup(res.msg, "error")
                        }
                    }
                )
            },
            submitEditForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        updateSupplier(this.editForm).then(res => {
                                res = res.data
                                if (res.code == 200) {
                                    popup("修改成功")
                                    this.editVisable = false
                                    this.editForm = {}
                                    this.init();

                                } else {
                                    popup(res.msg, "error")
                                }
                            }
                        )
                    }
                })

            }
        },
        mounted() {
            this.init()
        }
    }
</script>
<style>
    .table {
        background-color: #303133;
        height: 320px;
    }

    .pageUtils {
        position: absolute;
        width: 84%;
        margin: 2px auto;
        height: 43px;
    }

    .search {
        display: inline-block;
        margin: 0 auto;
        height: 51px;
        line-height: 51px;
    }

    .search .column {
        display: inline-block;
        margin: 0 auto;
        margin-right: 2px;
        line-height: 48px;
        height: 51px;
    }
</style>